package com.huawei.hmsdemo10086.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.huawei.hmsdemo10086.POJO.Car;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Shop;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ShopAdapter extends BaseAdapter{
    private List<Shop> datas = new ArrayList<Shop>();
    private Context context;
    private LayoutInflater layoutInflater;
    private List<Shop> choosed_data = new ArrayList<Shop>();

    public List<Shop> getChoosed_data() {
        return choosed_data;
    }

    public void clear_data()
    {
        choosed_data.clear();
    }

    public ShopAdapter(Context context, List<Shop> datas) {
        this.datas = datas;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size(); //返回列表的长度
    }

    @Override
    public Shop getItem(int position) {
        return datas.get(position); //通过列表的位置 获得集合中的对象
    }

    @Override
    public long getItemId(int position) { // 获得集合的Item的位置
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.shop_item, null);//找到布局文件
            convertView.setTag(new ShopAdapter.ViewHolder(convertView));
        }
        initViews(getItem(position), (ShopAdapter.ViewHolder) convertView.getTag());
        return convertView;

    }

    private void initViews(Shop data, ShopAdapter.ViewHolder holder) {//初始化数据


        holder.shop_id.setText(data.getShop_id() + "");//设置Tag

        //设置新闻标题为集合中获得的标题
        holder.shop_address.setText("地址：" + data.getShop_address());

        holder.shop_name.setText(data.getShop_name());

        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(10);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);

        Glide.with(context).load(data.getShop_src())
                .apply(options)
                .into(holder.shop_img);
    }

    protected class ViewHolder {
        private TextView shop_id;
        private TextView shop_name;
        private TextView shop_address;
        private ImageView shop_img;


        public ViewHolder(View view) {
            shop_id = (TextView) view.findViewById(R.id.shop_id);
            shop_name = (TextView) view.findViewById(R.id.shop_name);
            shop_address = (TextView) view.findViewById(R.id.shop_address);
            shop_img = view.findViewById(R.id.shop_img);
        }
    }
}
